t,n = map(int,input().split())
tws=[]
for i in range(n):
    temp= map(int,input().split())
    tws.append(temp)

def get_result(t,tws):
    max_i=len(tws)+1
    max_j=t+1

    dp=[[0 for j in range(max_j)] for i in range(max_i)]
    for i in range(max_i):
        for j in range(max_j):
            if i==0 or j==0:
                continue
            t,w=tws[i-1]
            if t>j:
                dp[i][j]=dp[i-1][j]
            else:
                dp[i][j]= max(dp[i-1][j],w+dp[i-1][j-t])
    return dp[-1][-1]

print(get_result(t,tws))